package com.example.tallybook;

import androidx.annotation.ContentView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.example.tallybook.databinding.ActivityJiZhangBinding;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class JiZhangActivity extends AppCompatActivity {
private ActivityJiZhangBinding binding;
private String[] mm = {
        "微信",
        "支付宝",
        "现金"
};
private String time;
private SQLiteDatabase sqLiteDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityJiZhangBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).setTitle("记录一下");
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        //初始化Spinner
        initSpinner();

        //设定时间
        time = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分 E").format(new Date());
        binding.time.setText(time);

        //创建并打开数据库
        sqLiteDatabase = new MySqlHold(getApplicationContext()).getWritableDatabase();

        //记录按钮，把信息存储到数据库中
        binding.button.setOnClickListener(v ->{
            if (binding.didian.getText().toString().equals("") || binding.money.getText().toString().equals("")){
                Toast.makeText(getApplicationContext(), "请填写信息（备注可写可不写）", Toast.LENGTH_SHORT).show();
            }else{
                addDate();
                Toast.makeText(getApplicationContext(), "记录成功", Toast.LENGTH_SHORT).show();
                binding.didian.setText("");
                binding.money.setText("");
                binding.beizhu.setText("");
            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initSpinner(){
        ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,mm);
        binding.spinner.setAdapter(arrayAdapter);
    }
    //往数据库中添加数据
    private void addDate(){
        ContentValues contentValues = new ContentValues();
        contentValues.put("id",0);
        contentValues.put("spinn",binding.spinner.getSelectedItem().toString());
        contentValues.put("time",time);
        contentValues.put("didian",binding.didian.getText().toString());
        contentValues.put("money",binding.money.getText().toString());
        contentValues.put("beizhu",binding.beizhu.getText().toString());
        sqLiteDatabase.insert("jilu",null,contentValues);
    }

}